package com.ifacebox.face.track.ui;

import javax.swing.*;

import com.sun.management.OperatingSystemMXBean;

import java.awt.*;
import java.lang.management.ManagementFactory;
import java.util.Objects;

/**
 * @author znn
 */
public class FaceStatusToolBar extends JToolBar {
    private final static int ONE_SECOND = 1000;
    private final static long ONE_MB = 1024 * 1024, ONE_GB = ONE_MB * 1024;
    private final static String OS_CPU_FORMAT = "CPU:%.2f%%", OS_MEMORY_FORMAT = "内存:%.2f%%", JVM_MEMORY_FORMAT = "JVM:%.2f%%";
    private final static String OS_MEMORY_INFO_FORMAT = "<html>内存总量：%.2fGB<br>空闲内存：%.2fGB</html>";
    private final static String JVM_MEMORY_INFO_FORMAT = "<html>JVM内存总量：%.2fGB<br>JVM空闲内存：%.2fGB<br>JVM最大内存：%.2fGB</html>";
    private final static String CLOCK_FORMAT = "%tT %<tA";
    private final static OperatingSystemMXBean OS = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
    private static final ImageIcon ICON = new ImageIcon(Objects.requireNonNull(FaceUserToolBar.class.getResource("/datebox.png")));
    private final JLabel clock;
    private JProgressBar cup, memory, jvm;

    public FaceStatusToolBar() {
        super.setFloatable(false);
        super.setLayout(new BorderLayout());
        super.add(this.clock = new JLabel(String.format(CLOCK_FORMAT, System.currentTimeMillis()), ICON, JLabel.LEFT), BorderLayout.WEST);
        super.add(new JPanel() {{
            this.setLayout(new FlowLayout(FlowLayout.RIGHT));
            this.add(cup = new JProgressBar() {{
                this.setStringPainted(true);
                this.setString(String.format(OS_CPU_FORMAT, 0.00));
            }});
            this.add(memory = new JProgressBar() {{
                this.setStringPainted(true);
                this.setString(String.format(OS_MEMORY_FORMAT, 0.00));
            }});
            this.add(jvm = new JProgressBar() {{
                this.setStringPainted(true);
                this.setString(String.format(JVM_MEMORY_FORMAT, 0.00));
            }});
        }}, BorderLayout.CENTER);
        new Timer(ONE_SECOND, (e) -> {
            double osCpu = OS.getSystemCpuLoad() * 100;
            long osTotalMemory = OS.getTotalPhysicalMemorySize(), osFreeMemory = OS.getFreePhysicalMemorySize();
            long jvmTotalMemory = Runtime.getRuntime().totalMemory(), jvmFreeMemory = Runtime.getRuntime().freeMemory(),
                    jvmMaxMemory = Runtime.getRuntime().maxMemory();
            this.cup.setValue((int) osCpu);
            this.cup.setString(String.format(OS_CPU_FORMAT, osCpu));
            this.memory.setMaximum((int) (osTotalMemory / ONE_MB));
            this.memory.setValue((int) ((osTotalMemory - osFreeMemory) / ONE_MB));
            this.memory.setString(String.format(OS_MEMORY_FORMAT, (double) (osTotalMemory - osFreeMemory) / osTotalMemory * 100));
            this.memory.setToolTipText(String.format(OS_MEMORY_INFO_FORMAT, (double) osTotalMemory / ONE_GB, (double) osFreeMemory / ONE_GB));
            this.jvm.setMaximum((int) (jvmTotalMemory / ONE_MB));
            this.jvm.setValue((int) ((jvmTotalMemory - jvmFreeMemory) / ONE_MB));
            this.jvm.setString(String.format(JVM_MEMORY_FORMAT, (double) (jvmTotalMemory - jvmFreeMemory) / jvmTotalMemory * 100));
            this.jvm.setToolTipText(String.format(JVM_MEMORY_INFO_FORMAT, (double) jvmTotalMemory / ONE_GB, (double) jvmFreeMemory / ONE_GB, (double) jvmMaxMemory / ONE_GB));
            this.clock.setText(String.format(CLOCK_FORMAT, System.currentTimeMillis()));
        }).start();
    }
}
